﻿// 2025/10/14: 首个版本
// 2025/11/11: 修正关闭对话框会移除原路径问题

using System;
using System.IO;
using Avalonia.Platform.Storage;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class OfflineMapPathDialog : DialogPanel
    {
        public OfflineMapPathDialog() // For designer
        {
            InitializeComponent();
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
        }

        public OfflineMapPathDialog(String path, String iconResourceName = null)
        {
            InitializeComponent();
            SetFixMode(350, 50, true);
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
            Icon = ResourceLoader.Load(iconResourceName)?.ToAvaloniaWindowIcon();
            Title = language["title"];

            MapPath = path;

            buttonPath.Content = String.IsNullOrEmpty(path) ? language["not-configured"] : Path.GetFileName(path);

            buttonPath.Click += async delegate
            {
                var selected = await this.GetStorageProvider().OpenFilePickerAsync(new FilePickerOpenOptions()
                {
                    Title = language["dialog-title"],
                    SuggestedStartLocation = await this.GetStorageProvider().TryGetWellKnownFolderAsync(WellKnownFolder.Desktop),
                    FileTypeFilter =
                    [
                        new FilePickerFileType(language["suffix"]){ Patterns = ["*.gmdb"] },
                    ]
                });
                if (selected == null || selected.Count == 0) return;

                MapPath = selected[0].Path.LocalPath;
                buttonPath.Content = Path.GetFileName(MapPath);
            };

            buttonRemove.Click += delegate
            {
                MapPath = null;
                buttonPath.Content = language["not-configured"];
            };
        }

        public String MapPath { get; set; }

        private LanguageSwitch language;
    }
}